# import csv
# import json
#
# import pymysql
# # 连接到MySQL数据库
# def connect_to_mysql():
#     try:
#         conn = pymysql.connect(
#             host="localhost",
#             user="root",
#             password="root",
#             database="graduate_database2"
#         )
#         return conn
#     except pymysql.connect.Error as err:
#         print("Error connecting to MySQL:", err)
#
# # 读取CSV文件
# def read_csv(filename):
#     data = []
#     with open(filename, 'r', encoding='utf-8') as file:
#         reader = csv.reader(file)
#         next(reader)  # 跳过标题行
#         for row in reader:
#             data.append(row)
#     return data
#
# # 插入数据到MySQL表中
# def insert_data(conn, data):
#     cursor = conn.cursor()
#     for row in data:
#         id_value = row[0].strip()  # 假设ID值在第一列
#         error_msg = row[1].strip()  # 假设错误消息在第二列
#         # 查询对应ID的行
#         query = "SELECT * FROM car_car WHERE id = %s"
#         cursor.execute(query, (id_value,))
#         result = cursor.fetchone()
#         if result:
#             # 如果找到了对应ID的行，则更新errormsg列
#             update_query = "UPDATE car_car t SET t.errormsg = %s WHERE t.id = %s"
#             cursor.execute(update_query, (error_msg, id_value))
#             print("已完成插入")
#         else:
#             print(f"ID {id_value} not found in the table.")
#     conn.commit()
#     cursor.close()
#
# # 主函数
# def main():
#     csv_filename = 'D:/Scrapy_data/car_ca8r.csv'
#     mysql_conn = connect_to_mysql()
#     if mysql_conn:
#         data = read_csv(csv_filename)
#         insert_data(mysql_conn, data)
#         mysql_conn.close()
#
# if __name__ == "__main__":
#     main()